package com.chinacache.maint.workorder.service;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.transaction.annotation.Transactional;

import com.chinacache.maint.commons.orm.hibernate.Page;
import com.chinacache.maint.commons.orm.hibernate.SimpleHibernateDao;
import com.chinacache.maint.workorder.entity.FlowFieldAttribute;

@Transactional
public class FlowFieldAttributeManager {
	private SimpleHibernateDao<FlowFieldAttribute, Integer> flowFieldAttributeDao;

	@Required
	public void setSessionFactory(SessionFactory sessionFactory) {
		flowFieldAttributeDao = new SimpleHibernateDao<FlowFieldAttribute, Integer>(
				sessionFactory, FlowFieldAttribute.class);
	}

	@Transactional(readOnly = true)
	public FlowFieldAttribute getFlowFieldAttribute(Integer id) {
		return flowFieldAttributeDao.findById(id);
	}

	@Transactional(readOnly = true)
	public Page<FlowFieldAttribute> getAllFlowFieldAttribute(
			Page<FlowFieldAttribute> page) {
		return flowFieldAttributeDao.findAll(page);
	}

	public void saveFlowFieldAttribute(
			FlowFieldAttribute flowFlowFieldAttributeAttribute) {
		flowFieldAttributeDao.save(flowFlowFieldAttributeAttribute);
	}

	public void deleteFlowFieldAttribute(Integer id) {
		FlowFieldAttribute flowFlowFieldAttributeAttribute = flowFieldAttributeDao
				.findById(id);
		flowFieldAttributeDao.delete(flowFlowFieldAttributeAttribute);
	}

	public void updateFlowFieldAttribute(
			FlowFieldAttribute flowFlowFieldAttributeAttribute) {
		flowFieldAttributeDao.update(flowFlowFieldAttributeAttribute);
	}

	@Transactional(readOnly = true)
	public List<FlowFieldAttribute> getAllFlowFieldAttribute() {
		return flowFieldAttributeDao.findAll();
	}

	@Transactional(readOnly = true)
	public FlowFieldAttribute getFlowFieldAttributeByFlowAndWorkOrderStepAndFieldAttribute(
			Integer flowId, Integer fieldAttributeId, Integer workOrderStepId) {
		String queryString = "select ffa from FlowFieldAttribute ffa where ffa.flow.id="
				+ flowId
				+ " and ffa.fieldAttribute.id="
				+ fieldAttributeId
				+ " and ffa.workOrderStep.id=" + workOrderStepId;
		return (FlowFieldAttribute) flowFieldAttributeDao
				.findUnique(queryString);
	}

	public List<FlowFieldAttribute> selectFlowFieldAttributeByWorkOrderStepId(
			Integer workOrderStepId, Integer flowId) {
		String queryString = "select ffa from FlowFieldAttribute ffa where ffa.flow.id="
				+ flowId + " and ffa.workOrderStep.id=" + workOrderStepId;
		return flowFieldAttributeDao
				.find(queryString);
	}
}
